package controllers;

import com.avaje.ebean.Page;
import models.AccountingRow;
import models.Category;
import models.ERowType;
import org.apache.commons.lang3.StringUtils;
import play.data.DynamicForm;
import play.data.Form;
import play.i18n.Messages;
import play.mvc.Controller;
import play.mvc.Result;
import utils.DateUtils;
import views.html.Accounting.byMonth;
import views.html.Accounting.edit;
import views.html.Accounting.list;

import java.text.SimpleDateFormat;
import java.util.List;

public class Accounting extends Controller {

    public static Result list(int page, String sortBy, String order, String filter) {
        Page<AccountingRow> AccountingRowPage = AccountingRow.page(page, 0, sortBy, order, filter);
        return ok(list.render(AccountingRowPage));
    }

    public static Result january(int year) {
        return byMonth(year, 1);
    }

    public static Result byMonth(int year, int month) {
        if (!DateUtils.checkYear(year) || !DateUtils.checkMonth(month)) {
            return notFound();
        }
        List<AccountingRow> accountingRows = AccountingRow.month(year, month);
        List<Category> recipeCategories = Category.findByType(ERowType.RECIPE);
        List<Category> expenseCategories = Category.findByType(ERowType.EXPENSE);

        return ok(byMonth.render(year, month, accountingRows, recipeCategories, expenseCategories));
    }

    public static Result add(String rowType, int year, int month) {

        try {
            ERowType.valueOf(StringUtils.upperCase(rowType));
        } catch (IllegalArgumentException ex) {
            return notFound();
        }
        if (!DateUtils.checkYear(year) || !DateUtils.checkMonth(month)) {
            return notFound();
        }
        Form<AccountingRow> accountingRowForm = Form.form(AccountingRow.class);
        return ok(edit.render(rowType, year, month, accountingRowForm));
    }

    public static Result save(String type, int year, int month) {


        if (!DateUtils.checkYear(year) || !DateUtils.checkMonth(month)) {
            return notFound();
        }

        Form<AccountingRow> accountingRowForm = Form.form(AccountingRow.class).bindFromRequest();
        DynamicForm requestData = Form.form().bindFromRequest();


        if (requestData.get("category.id").isEmpty()) {
            accountingRowForm.reject("category.id", "error.required");
        }


        int day = 1;
        if (requestData.get("day").isEmpty()) {
            accountingRowForm.reject("day", "error.required");
        } else {
            day = Integer.valueOf(requestData.get("day"));
            if (!DateUtils.checkDay(year, month, day)) {
                accountingRowForm.reject("day", "error.invalid");
            }
        }

        if (accountingRowForm.hasErrors()) {
            flash("error", Messages.get("alert.save.error"));
            return badRequest(edit.render(type, year, month, accountingRowForm));
        } else {
            AccountingRow accountingRow = accountingRowForm.get();
            accountingRow.date = DateUtils.createDate(year, month, day).getTime();
            accountingRow.save();
            Category category = Category.find.byId(accountingRow.category.id);
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            flash("success", Messages.get("alert.accounting.save.success", dateFormat.format(accountingRow.date), accountingRow.amount, category.name));
            return redirect(routes.Accounting.add(type, year, month));
        }
    }


}
